/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>Project: jse2204 - Reg9
 * <p>Powered by webrx On 2022-09-14 17:13:30
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Reg9 {
    public static void main(String[] args) {
        //
        String[] o = {"", "+", "-"};
        Random rand = new Random();
        int i = 0;
        int a = 0;
        int b = 0;
        while (true) {
             b++;
            //1.生成随机的表达式
            StringBuilder sbu = new StringBuilder("");
            for (int j = 1; j < 9; j++) {
                sbu.append(j).append("".concat(o[rand.nextInt(o.length)]));
            }
            sbu.append("9");

            //System.out.println(sbu);

            //2.使用正则表达式来计算结果是不是100
            Pattern p = Pattern.compile("-?\\d+");
            int sum = 0;
            Matcher m = p.matcher(sbu.toString());
            while (m.find()) {
                sum += Integer.parseInt(m.group());
            }
            //System.out.println(sum);

            if (sum == 100) {
                System.out.printf("%d、%s = 100(循环%d次)%n", ++i, sbu.toString(),b);
                a+=b;
                b = 0;
            }
            if (i == 10) {
                break;
            }

            sbu.delete(0, sbu.length());
        }

        System.out.println();
        System.out.println(a);


    }
}
